from django.db import models
from django.contrib.auth.models import User
from django.core.exceptions import SuspiciousOperation

from pirate_reputation.models import ReputationDimension


class PermissionsGroup(models.Model):
    name = models.CharField(max_length=70)
    description = models.TextField()
    #users = models.ManyToManyField(User) #django-nonrel does not support ManyToMany
    is_active = models.BooleanField()

    created_dt = models.DateTimeField(auto_now_add=True)
    modified_dt = models.DateTimeField(auto_now=True)


class Permission(models.Model):
    name = models.SlugField()
    description = models.TextField(null=True, blank=True)
    component = models.SlugField()
    component_membership_required = models.BooleanField(default=False)
    permissions_group = models.ForeignKey(PermissionsGroup)

    class Meta:
        unique_together = ("name", "component", "permissions_group")


class ReputationSpec(models.Model):
    dimension = models.ForeignKey(ReputationDimension, null=True)
    threshold = models.IntegerField()
    permissions_group = models.ForeignKey(PermissionsGroup)

    def save(self):
        if self.dimension == None:
            self.dimension = ReputationDimension.objects.null_dimension()
        super(ReputationSpec, self).save() 

